package com.exam.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.exam.pojo.Schedule;
import com.exam.pojo.Score;
import com.exam.pojo.Student;
import com.exam.pojo.Subject;
import com.exam.service.ScheduleService;
import com.exam.service.ScoreService;
import com.exam.service.StudentService;
import com.exam.service.SubjectService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 郭荣锋
  *  学生导航页
 * @param
 * @return
 */
@Controller
@RequestMapping("/StudentIndex")
public class StudentIndex {
	
	@Resource(name="mv")
	private ModelAndView mv;
	@Resource(name = "studentServiceImpl")
	private StudentService studentService;
	@Resource(name = "subjectServiceImpl")
	private SubjectService subjectService;
	@Resource(name = "scoreServiceImpl")
	private ScoreService scoreService;
	@Resource(name = "scheduleServiceImpl")
	private ScheduleService scheduleService;

	/**
	  *  学生端首页
	 * @param
	 * @return
	 */
	@RequestMapping("/studentIndex")
	public ModelAndView student(HttpServletRequest request,HttpServletResponse response) {

		int studentId = (Integer)request.getSession().getAttribute("studentId");
		Student student = studentService.getStudentById( studentId );
		List<Schedule> schedules = scheduleService.getScheduleByClassesId( student.getClasses().getId() );
		List<Score> scores = scoreService.getScoreBySno( student.getSno() );
		List<Subject> scoresSubject = new ArrayList<Subject>(  );

		List<Subject> subjects = new ArrayList<Subject>(  );
		for(int i = 0 ; i < schedules.size();i++){
			//判断所有要考的科目；
			Boolean addFlag = true;
			for(int j = 0;j < scores.size();j++){
				if( schedules.get( i ).getSubject().getId() == scores.get( j ).getSubjectId()){
					//判断该学生是否已经考过该科目，如果有，就不添加到显示的的列表
					addFlag = false;
					break;
				}
			}
			if(addFlag){
				subjects.add(  schedules.get( i ).getSubject() );
			}
		}

		for(int i = 0;i < scores.size();i++){
			//用做科目的显示
			scoresSubject.add( subjectService.getSubjectById( scores.get( i ).getSubjectId() ) );
		}

		mv.addObject( "student",student );
		mv.addObject( "subjects",subjects );
		mv.addObject( "scores",scores );
		mv.addObject( "scoresSubject",scoresSubject );

		mv.setViewName("admin/studentIndex");
		
		return mv;
	}
}
